package com.zhixun.mobile.modules.jiujiudevice;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderLog;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderRecord;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderLogService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderRecordService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.RequestUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.zhixun.mobile.modules.base.web.BaseController;
@Controller
@RequestMapping(value=Global.FRONT_PATH+"/testOrder")
public class JiujiuOrderFrontController  extends BaseController{
   private static final long USER_ID = 2l;
	 @Autowired
	private JiuOrderRecordService jiuOrderRecordService;
	 @Autowired
	 private JiuOrderLogService jiuOrderLogService;
	// ajax--分页加载订单
		@ResponseBody
		@RequestMapping(value = "ajaxOrder")
		public String ajaxOrder(HttpServletRequest request, HttpServletResponse response, Model model) {
			//支付状态 0-未支付，1-已支付
	        String states = RequestUtil.getString(request, "states");
            String openId = getOpenId("userinfo_openid", request);
			JiuOrderRecord param = new JiuOrderRecord();

			 if(StringUtils.isNotEmpty(states)){
				  param.setStatus(Integer.parseInt(states));
			 }else{
				  param.setStatus(null);
			 }
			param.setMemberOpenId(openId);
			param.setDelFlag(JiuOrderRecord.DEL_NO);
			Long pageNo = RequestUtil.getLong(request, "pageNo");
			if (pageNo == null) {
				pageNo = 1l;
			}
			Long pageSize = RequestUtil.getLong(request, "pageSize");
			if (pageSize == null) {
				pageSize = 5l;
			}
			Long allCount = jiuOrderRecordService.findAllCount(param);
			List<JiuOrderRecord> list = new ArrayList<JiuOrderRecord>(); 
			if ((allCount - (pageNo - 1) * pageSize) > 0) {
				Page<JiuOrderRecord> page = jiuOrderRecordService.find(new Page<JiuOrderRecord>(request, response), param);
				list = page.getList();
			}
			for (int i = 0; i < list.size(); i++) {
				JiuOrderRecord jiuOrderRecord = list.get(i);
				JiuOrderLog jiuOrderLog  =   jiuOrderLogService.findByOrderId(jiuOrderRecord.getId());
				if(jiuOrderLog != null && jiuOrderLog.getId() >0) {
					jiuOrderRecord.setJiuOrderLog(jiuOrderLog);
				}
			}
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("oList", list);
			writeJson(request, response, result);
			return null;

		}

		 /***
		  * 
		  * @function: <p>删除订单 </p>
		  * @param request
		  * @param response
		  * @param model
		  */
		@ResponseBody
		@RequestMapping(value="deleteOrder")
		 public  void deleteOrder(HttpServletRequest request, HttpServletResponse response, Model model){
			 Long orderId = RequestUtil.getLong(request, "id");
			 Map<String, Object> result = new HashMap<String, Object>();
			 JiuOrderRecord order = jiuOrderRecordService.get(orderId);
			 order.setDelFlag(JiuOrderRecord.DEL_YES);//设置删除
			 jiuOrderRecordService.save(order);
			 result.put("r", true);
			 result.put("m", "删除成功");
			 writeJson(request, response, result);
		 }
}
